package com.youdianstar.app.ui

import android.view.View
import com.youdianstar.app.R
import com.youdianstar.app.BR
import com.youdianstar.app.databinding.ActivityBusinessJoinBinding
import com.youdianstar.app.common.base.BaseActivity
import com.youdianstar.app.common.network.BaseApiResultObserver
import org.koin.androidx.viewmodel.ext.android.viewModel


/**
 * File: BusinessJoinActivity
 * Author: Assistant Create: 2024/12/19
 * Changes (from 2024/12/19)
 * --------------------------------------------------
 * description: 招商加盟页面
 * ---------------------------------------------------
 *
 */
class BusinessJoinActivity : BaseActivity<ActivityBusinessJoinBinding, BusinessJoinViewModel>() {

    override fun layoutId(): Int = R.layout.activity_business_join

    override fun initVariableId(): Int = BR.viewModel

    override fun initViewModel(): BusinessJoinViewModel {
        val model: BusinessJoinViewModel by viewModel()
        return model
    }

    override fun initViewLayout() {
        super.initViewLayout()
        
        mBinding.headBar.initTitle(this, "招商加盟", showInner = false)
        
        // 绑定数据
        mBinding.etName.setText(mViewModel.nameField.get())
        mBinding.etPhone.setText(mViewModel.phoneField.get())
        mBinding.etCity.setText(mViewModel.cityField.get())
        mBinding.etIntention.setText(mViewModel.intentionField.get())
    }

    override fun initLiveObserver() {
        super.initLiveObserver()
        
        mViewModel.submitResultLiveData.observe(this, object : BaseApiResultObserver<Any>(false) {
            override fun onDataResult(data: Any?) {
                super.onDataResult(data)
                dismissDialog()
                // 显示成功状态
                mViewModel.showSuccess.set(true)
                updateSuccessUI()
            }

            override fun onNetError(code: Int, e: String?) {
                super.onNetError(code, e)
                dismissDialog()
                toastShow(e ?: "提交失败，请稍后重试")
            }
        })
    }

    override fun initClickEvent() {
        // EditText内容变化监听
        mBinding.etName.setOnFocusChangeListener { _, hasFocus ->
            if (!hasFocus) {
                mViewModel.nameField.set(mBinding.etName.text.toString())
            }
        }
        
        mBinding.etPhone.setOnFocusChangeListener { _, hasFocus ->
            if (!hasFocus) {
                mViewModel.phoneField.set(mBinding.etPhone.text.toString())
            }
        }
        
        mBinding.etCity.setOnFocusChangeListener { _, hasFocus ->
            if (!hasFocus) {
                mViewModel.cityField.set(mBinding.etCity.text.toString())
            }
        }
        
        mBinding.etIntention.setOnFocusChangeListener { _, hasFocus ->
            if (!hasFocus) {
                mViewModel.intentionField.set(mBinding.etIntention.text.toString())
            }
        }
        
        // 提交按钮
        mBinding.btnSubmit.setOnClickListener {
            mViewModel.submitForm()
        }
        
        // 返回按钮
        mBinding.btnBack.setOnClickListener {
            finish()
        }
    }


    /**
     * 更新成功状态UI
     */
    private fun updateSuccessUI() {
        // 替换表单内容为成功提示
        val successView = View.inflate(this, R.layout.layout_business_join_success, null)
        mBinding.scrollView.visibility = View.GONE
        mBinding.container.removeAllViews()
        mBinding.container.addView(successView)
        
        // 返回按钮
        successView.findViewById<View>(R.id.btn_back_success)?.setOnClickListener {
            finish()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
    }

}
